package com.ccx.coachsite.versionupdate;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;


public class SoftVersionUtils {
	
	public static String VERSION_UPDATE_SETTING_NAME = "versionUpdate";//根据具体项目更改配置文件
	
	public static final String COMMON_URL_ENTRY = "http://sjupdate.sj.91.com/index.ashx?mt=4&qt=1502&fwversion=ALL&version=2.6&softid=6&branch=pandahome2";

	public static void checkForUpdate(Context ctx){
		
		SharedPreferences sp = ctx.getSharedPreferences(VERSION_UPDATE_SETTING_NAME, 0);
		//是否自动检测升级,一般在系统设置界面有一个开关可以设置
		boolean autoCheckForUpdate = sp.getBoolean("key_auto_check_version", true);
		if(autoCheckForUpdate){
			SoftUpdateTask task = new SoftUpdateTask(ctx, false);
			task.execute();
		}	
	}
	
	/**
	 * <br>Description:获取更新URL
	 * @param versionName
	 * @param softId
	 * @return
	 */
	public static String getUpdateUrl(String versionName, String softId) {
		StringBuffer sb = new StringBuffer(COMMON_URL_ENTRY);
		/*
		HttpRemoteRequest.appendAttrValue(sb, "mt", MT);
		HttpRemoteRequest.appendAttrValue(sb, "qt", "1502");
		HttpRemoteRequest.appendAttrValue(sb, "fwversion", "ALL");
		HttpRemoteRequest.appendAttrValue(sb, "version", versionName);
		HttpRemoteRequest.appendAttrValue(sb, "softid", softId);
		HttpRemoteRequest.appendAttrValue(sb, "branch", BRANCH);
		*/
		return sb.toString();
	}
	
	/**
	 * <br>Description:获取versionName
	 * @param context
	 * @param packageName
	 * @return
	 */
	public static String getVersionName(Context context, String packageName) {
		PackageInfo pInfo = null;
		String rs = "";
		try {
			pInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_META_DATA);
			rs = pInfo.versionName;
		} catch (Exception e) {
			e.printStackTrace();	
		}
		return rs;
	}
	
	/**
	 * 比较versionName,是否存在新版本
	 * @param newVersionName 新版本号
	 * @param oldeVersionName 	旧版本号
	 * @return 新版本号> 旧版本号  return true
	 */
	public static boolean isExistNewVersion(String newVersionName, String oldeVersionName) {
		if (oldeVersionName.toLowerCase().startsWith("v")) {
			oldeVersionName = oldeVersionName.substring(1, oldeVersionName.length());
		}
		if (newVersionName.toLowerCase().startsWith("v")) {
			newVersionName = newVersionName.substring(1, oldeVersionName.length());
		}

		if (oldeVersionName == null || newVersionName == null) {
			return false;
		}
		if (oldeVersionName.trim().length() == 0 || newVersionName.trim().length() == 0) {
			return false;
		}
		try {
			List<String> codes = parser(oldeVersionName.trim(), '.');
			List<String> versionCodes = parser(newVersionName.trim(), '.');
			for (int i = 0; i < codes.size(); i++) {
				if (i > (versionCodes.size() - 1)) {
					return false;
				}
				int a = Integer.parseInt(codes.get(i).trim());
				int b = Integer.parseInt(versionCodes.get(i).trim());
				if (a < b) {
					return true;
				} else if (a > b) {
					return false;
				}
			}
			if (codes.size() < versionCodes.size()) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 2.60.3=>[2,60,3]
	 * @param value
	 * @param c
	 * @return
	 */
	private static List<String> parser(String value, char c) {
		List<String> ss = new ArrayList<String>();
		char[] cs = value.toCharArray();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < cs.length; i++) {
			if (c == cs[i]) {
				ss.add(sb.toString());
				sb = new StringBuffer();
				continue;
			}
			sb.append(cs[i]);
		}
		if (sb.length() > 0) {
			ss.add(sb.toString());
		}
		return ss;
	}
	
}
